﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    public class GeneralTree<T>
    {
        public GeneralTreeNode<T> Root { get; set; }

        public IList<T> ToList()
        {
            IList<T> list = new List<T>();

            AddToList(list, Root);

            return list;
        }

        private void AddToList(IList<T> list, GeneralTreeNode<T> node)
        {
            list.Add(node.Value);

            foreach(GeneralTreeNode<T> child in node.Nodes)
            {
                AddToList(list, child);
            }
        }
    }

    public class GeneralTreeNode<T>
    {
        public GeneralTreeNode()
        {
            Nodes = new List<GeneralTreeNode<T>>();
        }

        public T Value { get; set; }

        public IList<GeneralTreeNode<T>> Nodes { get; set; }

        public IEnumerable<T> GetChildren()
        {
            return Nodes.Select(n => n.Value);
        }
    }
}
